<template>
  <PanelHand :route="route"></PanelHand>
  <div class="btns">
    <el-button @click="open(null)" type="primary" :icon="Plus" size="small">新增</el-button>
  </div>
 
  <el-table :data="tableData.list" style="width: 100%;">
    <el-table-column prop="id" label="id"></el-table-column>
    <el-table-column prop="name" label="昵称"></el-table-column>
    <el-table-column prop="permissionName" label="菜单权限" width="500px"></el-table-column>
    <el-table-column  label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
       
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination
      v-model:current-page="paginationData.pageNum"
      :page-size="paginationData.pageSize"
      :background="false"
      layout="total, prev, pager, next"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

  </div>
 
   <el-dialog  v-model="outerVisible" :before-close="beforeClose" title="添加权限" width="500px">

    <el-form ref="formRef" label-width="100px" label-position="left" :model="form" :rules="rules">
      <el-form-item v-show="false" prop="id">
      <el-input v-model="form.id"></el-input>
    </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="permissions">
        <el-tree ref="treeRef" :data="menuData" style="max-width: 600px;" node-key="id" show-checkbox :default-checked-keys="defaultKeys"
        :default-expanded-keys="[2]"
          
        />

      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
   </el-dialog>
</template>

<script setup>
import { nextTick, onMounted, reactive, ref } from 'vue'
import {userGetmenu,userSetmenu,menuList} from '@/api/index'
import {Plus} from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'; 

//控制弹窗显示
const outerVisible = ref(false)
const route=useRoute()
const form = reactive({
  id:'',
  name: '',
  permissions:'',
})
const treeRef=ref()
const formRef=ref()
//权限树形结构菜单数据
const menuData = ref([])
onMounted(()=>{
  userGetmenu().then(({data})=>{
    menuData.value = data.data
  }),
  getListData()
})

const rules=reactive({
  name:[
    {required:true,message:'请输入名称',trigger:'blur'}
  ]
})
//弹窗关闭的回调
const beforeClose = () => {
  //关闭弹窗
  outerVisible.value = false
  //重置表单
  formRef.value.resetFields()
  //
  treeRef.value.setCheckedKeys(defaultKeys)
}

const defaultKeys=[4,5]
const paginationData=reactive({
  pageNum:1,
  pageSize:10
})

const handleSizeChange=(val)=>{
  paginationData.pageSize=val
  getListData()
}
const handleCurrentChange=(val)=>{
  paginationData.pageNum=val
  getListData()
}

const tableData=reactive({
  list:[],
  total:0
})

//列表数据
const open=(rowData={})=>{
  outerVisible.value = true
  // form弹窗打开是异步
  nextTick(()=>{
    if(rowData){
      Object.assign(form,{id:rowData.id,name:rowData.name})
      treeRef.value.setCheckedKeys(rowData.permission)
    }
  })
}
//请求列表数据
const  getListData=()=>{
  menuList(paginationData).then(({data})=>{
    const {list ,total}=data.data
    tableData.list=list
    tableData.total=total
  })
}


const confirm=async (formEl)=>{
  if (!formEl) return
  await formEl.validate((valid,fields)=>{
    if(valid){
      const permissions=JSON.stringify(treeRef.value.getCheckedKeys())
       
      userSetmenu({name:form.name,permissions,id:form.id}).then(({data})=>{
        beforeClose()
        getListData()
      })
      
    }else{
      ElMessage.error('请输入正确的信息')
    }

  })

}

</script>

<style lang="less" scoped>
.btns{
  padding: 10px 0 10px 10px;
  background-color: #fff;
}

</style>